//
//  PlayerVC.swift
//  ListenFM
//
//  Created by 胡定锋Mac on 2017/8/10.
//  Copyright © 2017年 胡定锋. All rights reserved.
//

import UIKit
import Alamofire

class PlayerVC: BaseScene,UITableViewDelegate,UITableViewDataSource {


    var broadcast_id : NSString?
    var model: CommentModel?
    var headerView : UIView?
    
    var tableView = UITableView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        getRequest()
    }
    
    func setUpViews(){
        
        let rect = CGRect.init(x: 0, y: 200, width: SCR_WIDTH, height: SCR_HEIGHT-200)
        tableView = UITableView.init(frame: rect, style: UITableViewStyle.plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(UINib(nibName:"CommentsCell",bundle : nil), forCellReuseIdentifier: "commentscell")
        tableView.tableFooterView = UIView.init()
        view.addSubview(tableView)
        
        headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: SCR_WIDTH, height: 200))
        view.addSubview(headerView!)
        
//        let imageview :UIImageView = UIImageView.init(frame: CGRect.init(x: (SCR_WIDTH-70)/2, y: 10, width: 70, height: 70))
//        imageview.backgroundColor = UIColor.red
//        imageview.layer.cornerRadius = 35
//        imageview.clipsToBounds = true
//        headerView?.addSubview(imageview);
    
        
//        let animation = CABasicAnimation(keyPath: "transform.rotation.z")
//        //旋转360度 = PI*2
//        animation.fromValue = NSNumber.init(value: 0)
//        animation.toValue = M_PI*2
//        animation.isCumulative = true
//        animation.duration = 3
//        animation.repeatCount = 1
//        headerView?.layer.add(animation, forKey: "addLayerAnimationTranformRotationZ")
        
        //风车
        let imageView1 = UIImageView.init(frame: CGRect.init(x: (SCR_WIDTH-80)/2, y: 74, width: 80, height: 80))
        imageView1.layer.cornerRadius = 40
        imageView1.clipsToBounds = true
        imageView1.image = UIImage.init(named: "home_placeholder")
        headerView?.addSubview(imageView1)
        //transform.rotation.z  围绕z轴旋转   transform.rotation.y  围绕y轴旋转  transform.rotation.x  围绕x轴旋转
        let baseAnimation1 = CABasicAnimation.init(keyPath: "transform.rotation.z")
        baseAnimation1.fromValue = NSNumber.init(value: 0)
        baseAnimation1.toValue = NSNumber.init(value: 2*M_PI)
        baseAnimation1.duration = 10
        baseAnimation1.repeatCount = MAXFLOAT
        imageView1.layer.add(baseAnimation1, forKey: "home_placeholder")
        
        
    }

    //MARK: UITableViewDelegate
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (self.model?.data.count)!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return UITableViewAutomaticDimension
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

        return UITableViewAutomaticDimension
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell :CommentsCell = tableView.dequeueReusableCell(withIdentifier: "commentscell") as! CommentsCell
        cell.cellmodel = self.model?.data[indexPath.row]
        return cell
    }

    func getRequest() -> Void {
        
        Alamofire.request("http://yiapi.xinli001.com/fm/comment-list.json?broadcast_id=99390780&&key=c0d28ec0954084b4426223366293d190&offset=0&limit=10", parameters: nil)
            .responseJSON { response in
                if response.result.isSuccess {
                    
                    if let items = response.result.value as? NSDictionary{
                    
                        self.model = CommentModel.deserialize(from: items)
                        print(response);
                        self.setUpViews()
                        self.tableView.reloadData()
                    }
                }
        }
    }
}
